<template>
	<view>
		<view class="formbox">
			<view class="formbox-item1">
				<view class="title">工种</view>
				<view class="value">
					<picker @change="bindPickerType" :value="index" :range="typeNameList">
						<view class="uni-input">{{typeName}}</view>
					</picker>
					<image class="right" src="../../static/usercenter/icon_zk.png"></image>
				</view>
			</view>
			<view class="formbox-item1">
				<view class="title">预计到岗</view>
				<view class="value">
					<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
						<view class="uni-input">{{date}}</view>
					</picker>
					<image class="right" src="../../static/usercenter/icon_zk.png"></image>
				</view>
			</view>
			<view class="formbox-item1">
				<view class="title">期望地点</view>
				<view class="value" @click="addAdress">

					<view class="uni-input">{{addressValue}}</view>
					
				</view>
			</view>
			<view class="formbox-item1">
				<view class="title">期望薪资</view>
				<view class="value">
					<input type="text" v-model="money" placeholder-class="placeholderClass" placeholder="请填写期望薪资" />
				</view>
			</view>
		</view>
		<view class="fiexd-btn">
			<button @click="myPublish">发布</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				array1: ['工种1', '工种2', '工种3', '工种4'],
				array2: ['请选择地点', '160', '170', '180'],
				index: 0,
				index1: 0,
				date: "请选择到岗时间",
				typeNameList: [],
				typeList: [],
				typeName: '请选择工种',
				workId: '',
				addressList: [],
				addressNameList: [],
				addressId: '',
				addressValue:'请添加地址',
				money: ''
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},

		onLoad() {

		},

		onShow() {
			var addressValue = uni.getStorageSync("addressValue");
			var address = uni.getStorageSync("address");
			if(address!= ''){
				this.addressValue = addressValue;
				this.addressId  = address;
			}
			this.myWorkList();
			this.myWorkAddressList();
		},
		methods: {

			addAdress(){
				uni.navigateTo({
					url:'./addAddress'
				})
			},
			bindPickerType(e) {
				var index = e.target.value
				this.workId = this.typeList[index].id;
				this.typeName = this.typeList[index].workname;
			},
			bindPickerAddress(e) {
				var index = e.target.value
				this.addressId = this.addressList[index].id;
			},

			myWorkAddressList() {

				let param = {};
				let that = this;
				let addressNameList = [];
				this.$api.workAddressList(param).then(res => {

					if (res.code == 1) {
						that.addressList = res.data;

						for (var i = 0; i < res.data.length; i++) {
							addressNameList.push(res.data[i].name)
						}
						if (res.data.length == 0) {
							console.info("===地址为空===");
							// uni.showModal({
							// 	title: '地址提示',
							// 	content: '工作地址为空，请我的资料添加地址',
							// 	showCancel: false,
							// 	confirmText: '知道了',
							// 	success(res) {
							// 		if (res.confirm) {
							// 			uni.navigateTo({
							// 				url:'./addAddress'
							// 			})
							// 		}
							// 	}
							// })
						} else {
							that.addressId = that.addressList[index].id;
						}
					}
					console.info(res)
				})
				this.addressNameList = addressNameList;
			},

			myPublish() {

				let that = this;

				if (this.workId == '') {
					this.$util.$showToast("请选择工种", '', 'none');
					return;
				}
				if (this.date == '请选择到岗时间') {
					this.$util.$showToast("请选择到岗时间", '', 'none');
					return;
				}

				if (this.addressId == '') {
					this.$util.$showToast("请选择工作地点", '', 'none');
					return;
				}

				if (this.addressId == '') {
					this.$util.$showToast("请选择工作地点", '', 'none');
					return;
				}
				if (this.money == '') {
					this.$util.$showToast("请输入期望薪资", '', 'none');
					return;
				}
				let param = {
					worktype: this.workId,
					worktime: this.date,
					money: this.money,
					addressId: this.addressId

				};

				this.$api.publishWork(param).then(res => {


					if (res.code == 1) {
						that.$util.$showToast("发布成功", '', 'none');
						uni.navigateBack({
							delta: 1
						});
					} else {
						that.$util.$showToast(res.msg, '', 'none');

					}
					console.info(res)
				})
			},

			myWorkList() {

				let param = {};
				let that = this;
				let typeNameList = [];
				this.$api.worktype(param).then(res => {

					if (res.code == 1) {
						that.typeList = res.data;
						for (var i = 0; i < res.data.length; i++) {
							typeNameList.push(res.data[i].workname)
						}
					}

					console.info(res)
				})
				this.typeNameList = typeNameList;
			},

			bindDateChange: function(e) {
				this.date = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			}
		}
	}
</script>

<style lang="scss">
	.formbox {
		padding-bottom: 140rpx;

		.formbox-item1 {
			margin: 0 40rpx;
			padding: 35rpx 0;
			border-bottom: 1rpx solid #e0e0e0;

			.title {
				font-size: 28rpx;
				color: #333333;
				margin-bottom: 54rpx;
			}

			.value {
				display: flex;

				picker {
					font-size: 28rpx;
					color: #BFBFBF;
					flex: auto;
				}

				.right {
					width: 36rpx;
					height: 36rpx;
				}
			}

		}
	}
</style>
